package xmltest;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.io.*;
/**
 *
 * @author Randy
 */
public class xmlHandler extends DefaultHandler {
    private SAXAfdeling afdeling;
    private SAXHHS hhs;

    private boolean readingAfkorting;
    private boolean readingNaam;
    private boolean readingLocatie;

    public SAXAfdeling getAfdeling() {
        return this.afdeling;
    }

    public SAXHHS getHHS() {
        return this.hhs;
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        if (qName.equalsIgnoreCase("HHS")) {
            hhs = new SAXHHS();
        }
        else if (qName.equalsIgnoreCase("afdeling")) {
            afdeling = new SAXAfdeling(attributes.getValue(0));
            hhs.addAfdeling(afdeling);

        }
        else if (qName.equalsIgnoreCase("opleiding")) {
            
            SAXOpleiding opleiding = new SAXOpleiding();
            afdeling.addOpleiding(opleiding);
        }
        else if (qName.equalsIgnoreCase("afdnaam")) {
            
        }
        else if (qName.equalsIgnoreCase("afkorting")) {
            this.readingAfkorting = true;
        }
        else if (qName.equalsIgnoreCase("naam")) {
            this.readingNaam = true;
        }
        else if (qName.equalsIgnoreCase("locatie")) {
            this.readingLocatie = true;
        }
        else {
            System.out.println("Unknown element: " + qName);
        }
    }

    public void startDocument() {
    }

    public void endDocument() {
    }

    public void characters(char[] ch, int start, int length) {
        String chars = new String(ch, start, length).trim();
        if (chars.length() == 0) {
            return;
        }

        SAXOpleiding opleiding = afdeling.getLastOpleiding();
        if (readingAfkorting) {
            opleiding.setAfkorting(chars);
        }
        else if (readingNaam) {
            opleiding.setNaam(chars);
        }
        else if (readingLocatie) {
            opleiding.setLocatie(chars);
        }
    }

    public void endElement(String uri, String localName, String qName) {
        if (qName.equalsIgnoreCase("afkorting")) {
            this.readingAfkorting = false;
        }
        else if (qName.equalsIgnoreCase("naam")) {
            this.readingNaam = false;
        }
        else if (qName.equalsIgnoreCase("locatie")) {
            this.readingLocatie = false;
        }
    }
}
